package com.jf.cloud.service.sysmanager.dao.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.db.JdbcFactory;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.dao.ISysAppVersionService;
import com.jf.cloud.service.sysmanager.entity.SysAppVersion;
import com.jf.cloud.service.sysmanager.entity.SysAppVersionHistory;
import com.jf.cloud.service.sysmanager.entity.SysStaff;

/**
 * APP 版本管理 服务实现类
 * @author roway
 * @since 2017年12月12日下午4:15:59
 */
@Service
public class SysAppVersionServiceImpl extends BaseService<SysAppVersion, Long> implements ISysAppVersionService {

	@Resource
	private BaseDao baseDao;
	
	/*/**
	 * 修改员工  基本资料
	 * 
	 * @author 朱建文 创建时间：2017年12月5日 下午8:22:23
	 *//*
	@Transactional
	public void updateStaff(SysStaff staff) {
		  String updateSql = " update sys_staff a  set a.NICK_NAME=?,a.QQ=?,a.PHONE=?,a.UPDATE_TIME=?,"
		  		+ "a.STAFF_ACCOUNT_CODE=?,a.STAFF_NAME=?,a.STAFF_TYPE=? where ROW_ID=? ";
		  update(updateSql, staff.getNickName(),staff.getQq(),staff.getPhone(),DateUtil.getCurrentDateTime(),
				  staff.getStaffAccountCode(),staff.getStaffName(),staff.getStaffType(),staff.getRowId());
	}
	
	*//**
	 * 启用/停用员工
	 * 
	 * @author 朱建文 创建时间：2017年12月5日 下午8:22:14
	 *//*
	@Transactional
	public void stopStaff(SysStaff staff,SysStaff opt) {
		 int useFlag = staff.getUseFlag().equals("1")?0:1;
		 String updateSql = " update sys_staff a  set a.USE_FLAG=?,a.OPT_STAFF_ID=?,a.OPT_STAFF_ACCOUNT=?,a.UPDATE_TIME=? where ROW_ID=? ";
		 this.baseDao.update(updateSql, useFlag,opt.getRowId(),opt.getStaffAccount()
				 ,DateUtil.getCurrentDateTime(),staff.getRowId());
	}
	
	*//**
	 * 删除用户
	 * 
	 * @author 朱建文 创建时间：2017年12月5日 下午8:22:02
	 *//*
	@Transactional
	public void deleteStaff(SysStaff opt,Long id) {
		 String updateSql = " delete from  sys_staff  where ROW_ID = ? and USE_FLAG = 0 ";
		 this.baseDao.update(updateSql, id);
	}

	*//**
     * 检查是否重复
     * 
     * @author 朱建文 创建时间：2017年12月5日 下午8:21:52
     *//*
	public SysStaff checkIsRepeat(String staffAccount) {
		Query query = new Query();
		query.put("STAFF_ACCOUNT", staffAccount);
		query.put("DEL_FLAG", 0);
		return find(query); 
	}
	
	*//**
     * 重置登录密码
     * 
     * @author 朱建文 创建时间：2017年12月5日 下午8:21:38
     *//*
	public void resetPwd(SysStaff staff) {
		 String updateSql = " update sys_staff a  set a.STAFF_PWD=? where ROW_ID=? ";
		 update(updateSql, staff.getStaffPwd(),staff.getRowId());
	}
	
	*//**
	 * 修改用户密码
	 * 
	 * @author 朱建文 创建时间：2017年12月5日 下午8:21:27
	 *//*
	@Transactional
	public void updatePass(SysStaff staff, String password) {
		String sql = "UPDATE sys_staff SET STAFF_PWD = ? WHERE ROW_ID = ? AND STAFF_PWD = ?";
		this.baseDao.update(sql, password,staff.getRowId(),staff.getStaffPwd());
	}

	*//**
	 * 修改用户最后登录时间
	 * 
	 * @author 朱建文 创建时间：2017年12月5日 下午4:21:20
	 *//*
	@Override
	public void updateLastLoginTime(Long rowId) {
		String sql = "UPDATE sys_staff SET LAST_LOGIN_TIME = ? WHERE ROW_ID = ? ";
		this.baseDao.update(sql, DateUtil.getCurrentDateTime(), rowId);
	}

	*//**
	 * 员工帐号/手机号码查重
	 * 
	 * @author 朱建文 创建时间：2017年12月5日 下午8:21:09
	 *//*
	@Override
	public SysStaff findObject(String staffAccount, String phone, Integer rowId)throws Exception {
		SysStaff staff = null;
		if (staffAccount == null) {
			Query query = new Query();
			query.put("PHONE", phone);
			query.put("ROW_ID", rowId != null ? rowId:0).notEqual();
			staff = this.find(query);
		}
		if (phone == null) {
			Query query = new Query();
			query.put("STAFF_ACCOUNT", staffAccount);
			query.put("ROW_ID", rowId != null ? rowId:0).notEqual();
			staff = this.find(query);
		}
		return staff;
	}*/
	
	/**
	 * 根据主键查询版本维护
	 * 
	 * @author 绝峰代码机1.0 创建时间：2016-10-19
	 */
	public SysAppVersion findAppVersion(Long rowId) throws Exception {
		String sql = "SELECT * FROM sys_app_version WHERE ROW_ID = ?";
		return this.baseDao.findObject(sql, JdbcFactory.newInstance(SysAppVersion.class), rowId);
	}
	
	/**
	 * 新增/修改版本维护
	 * 
	 * @author 绝峰代码机1.0 创建时间：2016-10-19
	 */
	@Transactional
	public SysAppVersion saveOrUpdateAppVersion(SysAppVersion appVersion) throws Exception {
		Long rowId = appVersion.getRowId();
		if(rowId != null && rowId > 0) {
			appVersion = this.baseDao.updateAll(appVersion);
		} else {
			appVersion = this.baseDao.saveAll(appVersion);
		}
		return appVersion;
	}
	
	@Override
	public SysAppVersion saveAppVersion(SysAppVersion appVersion) {
		return this.baseDao.saveAll(appVersion);
	}
	
	@Override
	public SysAppVersionHistory saveAppVerSionHistoey(SysStaff employee,Long rowId) {
		SysAppVersionHistory history = new SysAppVersionHistory();
		history.setAppTypeId(Integer.valueOf(rowId+""));
		history.setEmpAccount(employee.getStaffAccount());
		history.setEmpNickName(employee.getStaffName());
		history.setCreateTime(DateUtil.getCurrentDateTime());
		return this.baseDao.saveAll(history);
	}

	@Override
	public Map<String, Object> getCanUseUpgradeInfo(int appId, int verCode, String channel) {
		StringBuffer buffer=new StringBuffer();

		buffer.append(" SELECT ");
		buffer.append("   a.VER_NAME AS verName, a.VER_CODE AS verCode, a.VER_DESC AS verDesc, ");
		buffer.append("   a.FILE_SIZE AS fileSize, a.DOWNLOAD_URL AS url,a.VER_STATE AS verState, ");
		buffer.append("   a.OLD_NOT_UPDATE AS oldNotUpdate,a.OLD_VER_FORCE AS oldVerForce ");
		buffer.append(" FROM sys_app_version a ");
		buffer.append(" WHERE ");
		buffer.append("   a.VER_STATE=1 AND a.PLATFORM_TYPE=? AND a.VER_CODE>? AND a.CHANNEL=? ");
		buffer.append(" ORDER BY a.VER_CODE DESC LIMIT 0,1");
		return baseDao.findMap(buffer.toString(), appId, verCode, channel);
	}


	/**
	 * 获得app是否需要显示静态页
	 * */
	@Override
	public String getControlFlag(int appId, String verCode,String channel,String appType,String verName) {
		String controlFlag="ahskljdhfiuawehfkjashdfuiawheflkjhasiufhaewlifsdgahsdlkjgvhalsiudhglakjsdhgviuaweglkjvbsdzkvjgaewgbvkjadsbgvkuagdslkjvbszdjkhgfliasdbvkjesgiuafgbkljsdbvl";
		StringBuffer buffer=new StringBuffer();
		String code = verName;
		if("10".equals(appType)){
			//ios
			code = verCode;
		}
		buffer.append(" SELECT a.CONTROL_FLAG ");
		buffer.append(" FROM sys_app_version a ");
		buffer.append(" WHERE ");
		buffer.append("   a.VER_STATE = 1 AND a.PLATFORM_TYPE = ? ");
		buffer.append("   AND a.CONTROL_FLAG LIKE '%"+code+"%' AND a.CHANNEL = ? AND a.APP_TYPE = ? ");
		Map<String, Object> result = new HashMap<String, Object>();

		result = baseDao.findMap(buffer.toString(), appId, channel, appType);
		if(result!=null){
			controlFlag = "skjadhfakljsdhflihsdjklfhauishgfkjalsdhgkuaasdfguywegkjfgaiuwegfiawgebfkjhvgasiufgakjgyigeriwhkljspqihjgbjhsgdvbeikjhsdjkhvkjlahgiuahjkhsl";
		}
		return controlFlag;
	}

}
